package com.wanmait.schedule.controller;

import com.wanmait.schedule.entity.Classroom;
import com.wanmait.schedule.entity.Role;
import com.wanmait.schedule.entity.Teacher;
import com.wanmait.schedule.service.ClassroomService;
import com.wanmait.schedule.util.AjaxResult;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 教室表 前端控制器
 * </p>
 *
 * @author wanmait
 * @since 2022-12-05
 */
@RestController
@RequestMapping("/manage/classroom")
@RequiresPermissions("manage:classroom")
public class ClassroomController {

    @Resource
    private ClassroomService classroomService;

    @GetMapping("list")
    public AjaxResult list1(){
        return AjaxResult.success(classroomService.listStudy());
    }

    //教室列表
    @PostMapping("list")
    public AjaxResult list(@RequestBody Classroom classroom,Integer pageNum){
        return AjaxResult.success(classroomService.list(classroom,pageNum));
    }

    //新增教室 修改教室
    @PostMapping("save")
    public AjaxResult save(@RequestBody Classroom classroom ,@RequestAttribute Teacher teacher){
        if(classroom.getId()==null){
            List<Classroom> classrooms = classroomService.list();
            for (Classroom i : classrooms) {
                if(i.getName().equals(classroom.getName())){
                    return AjaxResult.error(10001,"教室名已存在");
                }
            }
            classroom.setInputId(teacher.getId());
            classroomService.save(classroom);
        }
        else{
            classroomService.updateById(classroom);
        }
        return AjaxResult.success("保存成功",classroom);
    }

    //删除教室
    @GetMapping("delete")
    public AjaxResult delete(Integer id){
        classroomService.removeById(id);
        return AjaxResult.success("删除成功");
    }

    //根据id获得教室具体信息
    @GetMapping("findById")
    public AjaxResult findById(Integer id){
        Classroom classroom = classroomService.findById(id);
        return AjaxResult.success("查询成功",classroom);
    }

}
